package com.he1618.boot3.solr;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.request.QueryRequest;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;

import java.io.IOException;

public class App {

    public static void main(String[] args) throws SolrServerException, IOException {

        String solrUrl = "http://localhost:8983/solr";



        HttpSolrClient solrClient = new HttpSolrClient.Builder(solrUrl).withKerberosDelegationToken("").build();
//        Http2SolrClient client = new Http2SolrClient.Builder(solrUrl)
//                .withBasicAuthCredentials("solr", "SolrRocks").build();

//
//        for (int i = 1; i < 30; i++) {
//            SolrInputDocument doc = new SolrInputDocument();
//            doc.addField("id", ""+i);
//            doc.addField("title", "JuiceFS 社区版 v1.1-Beta 发布，新增五个实用功能");
//            doc.addField("content", "这是一个功能丰富的版本，带来了许多实用的新功能和改进。在这个版本中我们新增了以下功能：");
//
//
//            solrClient.add("demo", doc);
//            solrClient.commit("demo");
//        }

        QueryRequest req = new QueryRequest(new SolrQuery("*:*"));
        req.setBasicAuthCredentials("demo", "Asdf2345@");

        QueryResponse rsp = req.process(solrClient);
        System.out.println(rsp);

//        SolrQuery query = new SolrQuery("title:测试功能");
//        query.setRows(10);
//        query.setStart(0);
//        QueryResponse response = solrClient.query("demo", query);
        SolrDocumentList results = rsp.getResults();

        System.out.println(results.getNumFound());

        for (SolrDocument docItem : results) {
            // 处理搜索结果
            String id = (String) docItem.getFieldValue("id");
            String title = (String) docItem.getFieldValue("title");
            String content = (String) docItem.getFieldValue("content");
            System.out.println(title);
        }
    }
}
